package cn.myzf.ftp.cogradient.netty.clientplugins;

import cn.myzf.ftp.cogradient.netty.utils.PropertiesUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;

/**
 * @package cn.myzf.ftp.download.netty
 * @Date Created in 2019/12/29 17:03
 * @Author myzf
 */
public class CosClient {

    private volatile static COSClient cosClient;

    private CosClient() {
    }

    public static COSClient getCosClient() {
        if (cosClient == null) {
            synchronized (CosClient.class) {
                if (cosClient == null) {
                    String secretId = PropertiesUtil.getConfig("secretId");
                    String secretKey = PropertiesUtil.getConfig("secretKey");
                    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
                    Region region = new Region("ap-shanghai");
                    ClientConfig clientConfig = new ClientConfig(region);
                    cosClient = new COSClient(cred, clientConfig);
                }
            }
        }
        return cosClient;
    }

    public static void closeClient() {
        if (cosClient != null) {
            cosClient.shutdown();
        }
    }
}